
/**
 * Date:2021年9月28日下午2:21:30
 * Copyright (c) 2021, www.bwbroad.com All Rights Reserved.
 *
*/

package com.example.demo;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.alibaba.fastjson.JSON;

import redis.clients.jedis.Jedis;



/**
 * Description: TODO <br/>
 * Date:     2021年9月28日 下午2:21:30 <br/>
 * @author   wangbing	 
 */
public class DefaultPositionHandler implements IPositionHandler{


    private Map<String, String> cache = new ConcurrentHashMap<>();

    @Override
    public BinlogPositionEntity getPosition(SyncConfig syncConfig) {
    	 //连接本地的redis
        Jedis jedis = getRedis();
        System.out.println("getRedis   key="+generateKey(syncConfig)+",value ="+jedis.get(generateKey(syncConfig)));
        return JSON.parseObject(jedis.get(generateKey(syncConfig)), BinlogPositionEntity.class);
    }

    @Override
    public void savePosition(SyncConfig syncConfig, BinlogPositionEntity binlogPositionEntity) {
    	 //连接本地的redis
        Jedis jedis = getRedis();
        System.out.println("setRedis   key="+generateKey(syncConfig)+",value ="+JSON.toJSONString(binlogPositionEntity));
//        cache.put(generateKey(syncConfig), JSON.toJSONString(binlogPositionEntity));
        jedis.set(generateKey(syncConfig), JSON.toJSONString(binlogPositionEntity));
    }


    /**
     * @param syncConfig 参数配置
     * @return 生成的key
     */
    private String generateKey(SyncConfig syncConfig) {
        return syncConfig.getHost() + ":" + syncConfig.getPort();
    }
    private Jedis getRedis() {
    	 //连接本地的redis
        Jedis jedis = new Jedis("localhost");
        System.out.println(jedis.getClient().getPort());
        return jedis;
    }

}

